﻿' ----------------------------------------------------------------------------------
' Microsoft Developer & Platform Evangelism
' 
' Copyright (c) Microsoft Corporation. All rights reserved.
' 
' THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, 
' EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES 
' OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
' ----------------------------------------------------------------------------------
' The example companies, organizations, products, domain names,
' e-mail addresses, logos, people, places, and events depicted
' herein are fictitious.  No association with any real company,
' organization, product, domain name, email address, logo, person,
' places, or events is intended or should be inferred.
' ----------------------------------------------------------------------------------

Imports Microsoft.Samples.WindowsPhoneCloud.Phone.ViewModel
Imports System.Globalization

Namespace Pages

    Partial Public Class RegisterPage
        Inherits PhoneApplicationPage
        Public Sub New()
            Me.InitializeComponent()

            Me.ViewModel = New RegisterPageViewModel()
        End Sub

        Public Property ViewModel() As RegisterPageViewModel
            Get
                Return TryCast(Me.DataContext, RegisterPageViewModel)
            End Get
            Set(ByVal value As RegisterPageViewModel)
                Me.DataContext = value
            End Set
        End Property

        Private Sub OnRegisterButtonClick(ByVal sender As Object, ByVal e As RoutedEventArgs)
            If Me.ViewModel.IsRegisterEnabled Then
                Me.ViewModel.Register(
                    Sub()
                        ' Save the username of the last user registered.
                        MessageBox.Show(String.Format(CultureInfo.CurrentCulture, "User {0} successfully registered.", Me.ViewModel.UserName), "Registration Successful", MessageBoxButton.OK)

                        ' Clean up authentication token (in case there is any available).
                        App.CloudClientFactory.CleanAuthenticationToken()

                        App.CloudClientFactory.SetUserName(Me.ViewModel.UserName)
                        Me.NavigationService.GoBack()
                    End Sub, Sub(msg) MessageBox.Show(msg, "Registration Error", MessageBoxButton.OK))
            Else
                MessageBox.Show("Please fill out all the information in the registration page.", "Registration Incomplete", MessageBoxButton.OK)
            End If
        End Sub

        Private Sub OnTextBoxKeyUp(ByVal sender As Object, ByVal e As KeyEventArgs)
            If e.Key = Key.Enter Then
                If sender Is Me.UserNameTextBox Then
                    Me.EMailTextBox.Focus()
                ElseIf sender Is Me.EMailTextBox Then
                    Me.PasswordBox.Focus()
                ElseIf sender Is Me.PasswordBox Then
                    Me.ConfirmPasswordBox.Focus()
                Else
                    Me.RegisterButton.Focus()
                End If
            End If
        End Sub
    End Class
End Namespace